package com.hspedu.single_;

/**
 * 懒汉式
 */
public class SingleTon02 {
    public static void main(String[] args) {
        Cat instance = Cat.getInstance();
        System.out.println(instance);

        Cat instance2 = Cat.getInstance();
        System.out.println(instance2);

        System.out.println(instance == instance2);

    }
}

//【单例模式-懒汉式】
class Cat {

    private String name;
    //2.定义一个静态属性对象
    private static Cat cat;

    //1.构造器私有化
    private Cat(String name) {
        this.name = name;
    }

    //3.提供一个公共静态方法，返回 Cat 对象
    public static Cat getInstance() {
        if (cat == null) {//如果还没有创建 Cat 对象
            cat = new Cat("小可爱");
        }

        return cat;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}